<template>
  <initVanta 
    :color="vantaData.hexToNumber(selectedColor)" 
    :effect="selectedEffect"
    ref="vantaBg"
  />
  <div class="header">
    <backButton/>
  </div>
  <div id="conDraftReports">
    <h1>Batch Confirm Draft Reports</h1>
    <div class="parent-flexbox">
      <div class="switch-container">
        <div class="checkbox-wrapper-35">
          <input 
            type="checkbox" 
            id="switch" 
            v-model="isPRD" 
            @change="handleAddressSwitch" 
            class="switch"
          />
          <label for="switch">
            <span class="switch-x-text">GPO </span>
            <span class="switch-x-toggletext">
              <span class="switch-x-unchecked"><span class="switch-x-hiddenlabel">Unchecked: </span>UAT</span>
              <span class="switch-x-checked"><span class="switch-x-hiddenlabel">Checked: </span>PRD</span>
            </span>
          </label>
        </div>
        <div class="checkbox-wrapper-35">
          <input 
            type="checkbox" 
            id="headless-switch" 
            v-model="isHeadless" 
            @change="handleStepsSwitch" 
            class="switch"
          />
          <label for="headless-switch">
            <span class="switch-x-text">Steps </span>
            <span class="switch-x-toggletext">
              <span class="switch-x-unchecked"><span class="switch-x-hiddenlabel">Unchecked: </span>Hide</span>
              <span class="switch-x-checked"><span class="switch-x-hiddenlabel">Checked: </span>Show</span>
            </span>
          </label>
        </div>
      </div>
      <!-- 输入框 -->
      <div class="form-container">
        <form class="form">
          <div class="form-group">
            <textarea v-model="orderNumbers" required cols="50" rows="10" id="textarea" name="textarea" placeholder="请输入多个单号，每个单号独占一行"></textarea>
          </div>
        </form>
      </div>
      <button class="button type1" @click="navigateToGPO">
        <span class="btn-txt">START</span>
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import backButton from '../../../src/components/backButton.vue'; // 引入 BackButton 组件
import { ref } from '@vue/reactivity';
import { ElMessage } from 'element-plus';
import initVanta from '../../../src/components/initVanta.vue';
import { useVanta } from '../../components/useVanta';

const { selectedColor, selectedEffect, hexToNumber } = useVanta();

const vantaData = useVanta();

declare const fileAPI: any;

// 数据及状态
const orderNumbers = ref('');
const selectedAddressIndex = ref('PRD');
const isPRD = ref(true);
const isHeadless = ref(false); // 默认为后台模式

async function navigateToGPO() {
  if (orderNumbers.value.trim() === '') {
    ElMessage.error('请输入单号');
    return;
  }

  // 使用从预加载脚本暴露的API
  const environment = selectedAddressIndex.value;
  const address = environment === 'PRD' ? 'http://cnapp.sgs.net/gpo-web/#/' : 'http://cnapp-uat.sgs.net/sgsLogin/';
  const orderNumbersArray = orderNumbers.value.split('\n').map((num: string) => num.trim()).filter((num: string) => num);
  try {
    await fileAPI.navigateWithPlaywright(address, orderNumbersArray, isHeadless.value);
    ElMessage.success('Processing, Please wait...');
  } catch (error) {
    ElMessage.error('Failed, Please check your input.');
    console.error(error);
  }
}

function handleAddressSwitch() {
  // selectedAddressIndex.value = value;
  selectedAddressIndex.value = isPRD.value ? 'PRD' : 'UAT';
}
function handleStepsSwitch() {
  console.log(`Headless mode is now ${isHeadless.value ? 'enabled' : 'disabled'}`);
}
</script>

<style lang='less' scoped>
.parent-flexbox {
  align-items: center;
  display: flex;
  justify-content: center; /* 或者使用 'flex-start'、'center'、'flex-end' 等，根据需要调整左右间距 */
  gap: 80px; /* 增加间距 */
  flex: 1;
}
.parent-flexbox > div {
  margin-left: 42px; /* 根据需要调整值 */
}

.orderInput {
  border-radius: 10px; /* 增大圆角 */
  padding: 15px; /* 增加内边距 */
  height: 50px; /* 增加高度 */
  flex: 2;

  &:focus {
    border-color: #E91E63; /* 聚焦时改变边框颜色 */
  }
}

.custom-switch {
  --el-switch-on-color: #FF66CC; /* PRD 状态的颜色 */
  --el-switch-off-color: #FFC7D2; /* UAT 状态的颜色 */
}

.form-container {
  width: 400px;
  background: linear-gradient(#212121, #212121) padding-box,
              linear-gradient(145deg, transparent 35%,#e81cff, #40c9ff) border-box;
  border: 3px solid transparent;
  padding: 24px 24px;
  font-size: 14px;
  font-family: inherit;
  color: white;
  display: flex;
  flex-direction: column;
  gap: 20px;
  box-sizing: border-box;
  border-radius: 16px;
  background-size: 200% 100%;
  animation: gradient 5s ease infinite;
}

.form-container .form-group textarea {
  width: 90%;
  padding: 10px 10px;
  border-radius: 8px;
  resize: none;
  color: #fff;
  height: 90px;
  // border: 1px solid #414141;
  background-color: transparent;
  font-family: inherit;
  transition: border-color 0.3s ease; /* 平滑过渡 */
}

.form-container .form-group textarea:focus {
  outline: none;
  border-color: #e81cff;
}

/* From Uiverse.io by Gaurav-WebDev */ 
.button {
  height: 50px;
  width: 200px;
  position: relative;
  background-color: transparent;
  cursor: pointer;
  border: 2px solid #252525;
  overflow: hidden;
  border-radius: 30px;
  color: #333333;
  transition: all 0.5s ease-in-out;
}

.btn-txt {
  z-index: 1;
  font-weight: 800;
  letter-spacing: 4px;
}

.type1::after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  transition: all 0.5s ease-in-out;
  background-color: #f826c7;
  border-radius: 30px;
  visibility: hidden;
  height: 10px;
  width: 10px;
  z-index: -1;
}

.button:hover {
  box-shadow: 1px 1px 200px #252525;
  color: #fff;
  border: none;
}

.type1:hover::after {
  visibility: visible;
  transform: scale(100) translateX(2px);
}

/* Switch Button */
.checkbox-wrapper-35 .switch {
  display: none;
}

.checkbox-wrapper-35 .switch + label {
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  color: #27272f;
  cursor: pointer;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-size: 12px;
  line-height: 15px;
  position: relative;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.checkbox-wrapper-35 .switch + label::before,
.checkbox-wrapper-35 .switch + label::after {
  content: '';
  display: block;
}

.checkbox-wrapper-35 .switch + label::before {
  background-color: #05012c;
  border-radius: 500px;
  height: 20px; /* 调整高度 */
  margin-right: 8px;
  -webkit-transition: background-color 0.125s ease-out;
  transition: background-color 0.125s ease-out;
  width: 40px; /* 调整宽度 */
}

.checkbox-wrapper-35 .switch + label::after {
  background-color: #fff;
  border-radius: 13px;
  box-shadow: 0 3px 1px 0 rgba(37, 34, 71, 0.05), 0 2px 2px 0 rgba(37, 34, 71, 0.1), 0 3px 3px 0 rgba(37, 34, 71, 0.05);
  height: 18px; /* 调整高度 */
  left: 1px; /* 调整位置 */
  position: absolute;
  top: 1px;
  -webkit-transition: -webkit-transform 0.125s ease-out;
  transition: -webkit-transform 0.125s ease-out;
  transition: transform 0.125s ease-out;
  transition: transform 0.125s ease-out, -webkit-transform 0.125s ease-out;
  width: 18px; /* 调整宽度 */
}

.checkbox-wrapper-35 .switch + label .switch-x-text {
  display: block;
  margin-right: .3em;
}

.checkbox-wrapper-35 .switch + label .switch-x-toggletext {
  display: block;
  font-weight: bold;
  height: 15px;
  overflow: hidden;
  position: relative;
  width: 40px;
}

.checkbox-wrapper-35 .switch + label .switch-x-unchecked,
.checkbox-wrapper-35 .switch + label .switch-x-checked {
  left: 0;
  position: absolute;
  top: 0;
  -webkit-transition: opacity 0.125s ease-out, -webkit-transform 0.125s ease-out;
  transition: opacity 0.125s ease-out, -webkit-transform 0.125s ease-out;
  transition: transform 0.125s ease-out, opacity 0.125s ease-out;
  transition: transform 0.125s ease-out, opacity 0.125s ease-out, -webkit-transform 0.125s ease-out;
}

.checkbox-wrapper-35 .switch + label .switch-x-unchecked {
  opacity: 1;
  -webkit-transform: none;
  transform: none;
}

.checkbox-wrapper-35 .switch + label .switch-x-checked {
  opacity: 0;
  -webkit-transform: translate3d(0, 100%, 0);
  transform: translate3d(0, 100%, 0);
}

.checkbox-wrapper-35 .switch + label .switch-x-hiddenlabel {
  position: absolute;
  visibility: hidden;
}

.checkbox-wrapper-35 .switch:checked + label::before {
  background-color: #ffb500;
}

.checkbox-wrapper-35 .switch:checked + label::after {
  -webkit-transform: translate3d(20px, 0, 0); /* 调整滑块位置 */
  transform: translate3d(20px, 0, 0);
}

.checkbox-wrapper-35 .switch:checked + label .switch-x-unchecked {
  opacity: 0;
  -webkit-transform: translate3d(0, -100%, 0);
  transform: translate3d(0, -100%, 0);
}

.checkbox-wrapper-35 .switch:checked + label .switch-x-checked {
  opacity: 1;
  -webkit-transform: none;
  transform: none;
}

.switch-container{
  display: flex;
  flex-direction: column; /* 修改为纵向排列 */
  align-items: flex-start; /* 可选：对齐方式 */
}

.checkbox-wrapper-35 {
  margin-bottom: 10px; /* 增加上下间距 */
}

.header {
  display: flex;
  justify-content: space-between; /* 将内容和按钮分开 */
  align-items: center; /* 垂直居中对齐 */
  margin-bottom: 20px; /* 添加底部间距 */
  width: 100%; /* 确保 header 占据整个宽度 */
}

/* back button */ 
button-back {
  align-items: center;
  background-color: transparent;
  color: #fff;
  cursor: pointer;
  display: flex;
  font-family: ui-sans-serif,system-ui,-apple-system,system-ui,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";
  font-size: 1rem;
  font-weight: 700;
  line-height: 1.5;
  text-decoration: none;
  text-transform: uppercase;
  outline: 0;
  border: 0;
  padding: 1rem;
}

button-back:before {
  background-color: #fff;
  content: "";
  display: inline-block;
  height: 1px;
  margin-right: 10px;
  transition: all .42s cubic-bezier(.25,.8,.25,1);
  width: 0;
}

button-back:hover:before {
  background-color: #fff;
  width: 3rem;
}
</style>